package intermedia;

import java.util.ArrayList;

import semantica.SymbolTable;

public class Graph {

	private String id;
	private ArrayList<Node> nodes;
	private Attributes attributes;
	private boolean digraph;

	public Graph(String _id, boolean digraph) {
		if (_id == null)
			id = "";
		else
			id = _id;
		nodes = new ArrayList<Node>();
		attributes = new Attributes();
		this.digraph = digraph;
	}

	public boolean isDigraph() {
		return digraph;
	}

	public String getId() {
		return id;
	}

	public void addNode(Node _node) {
		nodes.add(_node);
	}

	public Node getNode(String node_id, SymbolTable st) {

		for (int i = 0; i < nodes.size(); i++) {
			if (nodes.get(i).getId().equals(node_id))
				return nodes.get(i);
		}

		Node node = new Node(node_id, st);
		addNode(node);

		return node;
	}

	public boolean nodeExists(String node_id) {
		for( Node node: nodes){
			if( node.getId().equals(node_id))
				return true;
		}
		return false;
	}

	public void addAttribute(String key, String val, int line, int column)
			throws InvalidTypeAttributeException {
		attributes.addAtribute(key, val, line, column);
	}

	public ArrayList<Node> getNodes() {
		return nodes;
	}

	public void setEdgesVisited(boolean v) {
		for (Node node : nodes) {
			node.setEdgesVisited(v);
		}

	}

	public Attributes getAttributes() {
		return attributes;
	}
	
	public void addGraphAttributes(Attributes attr){
		attributes.addAtributes(attr);
	}
}
